{% extends "master.html" %}
{% load common_extras %}
{% block head_ext %}
<script type="text/javascript" src="/site_media/js/rpc.js"></script>
<script type="text/javascript" src="/site_media/js/jquery/ui/ui.core.js"></script>
<script type="text/javascript" src="/site_media/js/jquery/ui/ui.dialog.js"></script>
<link rel="stylesheet" href="/site_media/js/jquery/ui/themes/flora/flora.all.css" type="text/css" media="screen" title="no title" />
<meta http-equiv="content-type" content="text/html; charset=utf8" />
<script type="text/javascript">
     
  service = new rpc.ServiceProxy("/services/admin/", {asynchronous:false, methods: ['add_type','delete_type']});
  $(document).ready(function(){
	  $('input.addtype').click(function(){
		  $('#add_form input[id=name]').val("");
		  $('#add_form input[id=orders]').val("0");
		  $('#add_form select[id=pid]').val("");
		  $('#add_form input[id=id]').val("");
		  $("#add_dialog").show().dialog({
		     height: 230, width: 330, modal: true,
	         title: '页面分类',
		     overlay: {
	            opacity: 0.5, 
	            background: "black" 
	         }
	     });
	  });
	  
	  $('#add_form input.button').click(function(){
		  	//添加,修改 分类信息
	        var name =  $('#name').val();
	        var orders = $('#orders').val();
	        var pid = $('#pid').val();
	        var id = Number($('#id').val());
		    if(!name){
			   alert("请输入分类名称");
			}else if(!orders){
				alert("请输入排名");
			}else if(!pid){
				alert("父分类出错");
			}else{
			    try{
			    	code = "页面报告异常";
			    	if ( !id ) {
			    		//添加分类
			    		code = service.add_type({'name': name,'orders':orders,'pid':pid});
			    	}else if (id > 0) {
			    		//修改页面
			    		code = service.add_type({'id':id,'name': name,'orders':orders,'pid':pid});
			    	}
			    	if (code == 1){
			    		alert("添加分类成功");
			    		window.location.reload() ;
			    	}else if(code==2){ 
			    		alert("修改分类成功");
			    		window.location.reload() ;
			    	}else{
			    		alert(code);
			    	}
				}catch(e){
				   alert(e.message);
				}
		    }
		});
	  
	  $('a.edit').click(function() {
	    	var id = $(this).parent().parent().attr('id');
	    	var name = $(this).parent().parent().attr('name');
	    	var orders = $(this).parent().parent().attr('orders');
	    	var pid = $(this).parent().parent().attr('pid');
	    	$('#add_form input[id=name]').val(name);
			$('#add_form input[id=orders]').val(orders);
	    	$('#add_form select[id=pid]').val(pid);
	    	$('#add_form input[id=id]').val(id);
	    	alert($('#add_form input[id=id]').val());
	    	$("#add_dialog").show().dialog({
			     height: 230, width: 330, modal: true,
		         title: '页面添加',
			     overlay: {
	               opacity: 0.5, 
	               background: "black" 
	            }
		     });
	    });
	  
	  $('a.delete').click(function() {
	    	var id = $(this).parent().parent().attr('id');
	    	var pid = $(this).parent().parent().attr('pid');
	    	if(!pid || !id) {
	    		alert("id 或者 pid不正确");
	    		return ;
	    	}
	    	if(pid != -1) {
	    		if(confirm('确定删除该子分类吗？')){
	    			code = service.delete_type(id);	    			
	    		}
	    	}else {
	    		if(confirm('确定删除该父分类,并且删除之下的全部子分类吗？')) {
	    			code = service.delete_type(id);
	    		}
	    	}
	    	if(code == 1) {
	    		alert('删除成功');
	    		window.location.reload() ;
	    	}else{
	    		alert(code);
	    	}
	    });
  });
</script>
{% endblock %}
{% block title %}类型分类{% endblock %}
{% block content %}
<input type="button" class="addtype" value="添加分类"/><br/>
<table cellspacing="1" class="tablesorter">
  <thead>
    <th>分类ID</th>
    <th>名称</th>
    <th>排行</th>
    <th>所属分类</th>
    <th>修改 </th>
	<th>删除</th>
  </thead>
  <tbody>
    {% if parent_type %}
      {% for ptype in parent_type %}
       <tr id="{{ptype.id}}" name="{{ptype.name}}" orders="{{ptype.orders}}" pid="{{ptype.pid}}">
         <td>{{ ptype.id }}</td>
         <td>{{ ptype.name }}</td>
         <td>{{ ptype.orders}}</td>
         <td>父分类</td>
         <td><a href="#" class="edit">编辑</a></td>
		 <td><a href="#" class="delete">删除</a></td>
       </tr>
      {% endfor %}
    {% endif %}
  </tbody>
</table>
<hr/>
<table cellspacing="1" class="tablesorter">
  <thead>
    <th>分类ID</th>
    <th>名称</th>
    <th>排行</th>
    <th>所属分类</th>
    <th>修改 </th>
	<th>删除</th>
  </thead>
  <tbody>
    {% if son_type %}
      {% for stype in son_type %}
       <tr id="{{stype.id}}" name="{{stype.name}}" orders="{{stype.orders}}" pid="{{stype.pid}}">
         <td>{{ stype.id }}</td>
         <td>{{ stype.name }}</td>
         <td>{{ stype.orders}}</td>
         <td>{{ stype.pname }}</td>
         <td><a href="#" class="edit">编辑</a></td>
		 <td><a href="#" class="delete">删除</a></td>
       </tr>
      {% endfor %}
    {% endif %}
  </tbody>
</table>
<div id="add_dialog" class="flora" style="display: none;">
    <form id="add_form">
    	<p>
    		<input type="hidden" id="id" name="id" />
            <span>分类名称:<input id="name" type="text" value="" size="30" /></span><br>
			<br>
			<span>排&nbsp;&nbsp;&nbsp;&nbsp;行:<input id="orders" type="text" value="" size="30" value="0"/></span><br><br>
			<span>所属分类:
				<select id="pid">
					<option value="-1">父分类</option>
					{% if parent_type %}
						{% for ptype in parent_type %}
							<option value="{{ ptype.id }}">{{ ptype.name }}</option>
					    {% endfor %}
					{% endif %}
				</select>
			</span><br><br>
			<input class="button" type="button" value="提交" />
		</p>
    </form>
</div>
{% endblock %}